import sys

def f7(seq):
    seen = set()
    seen_add = seen.add
    return [ x for x in seq if x not in seen and not seen_add(x)]

class DataModel:
   db_nodes = 0
   slaves = 0
   queriesSlave = 0
   timeFromTestAverage = 0
   queriesPerSecAverage = 0
   standardDeviation = 0

# recursing through the list of 
# 
def catchBlocks(arr):
   if len(arr) < 27:
      return []
   else:
      return ([readBlock(arr[:27])] + catchBlocks(arr[27:]))

#returning a 
#input param l stands for line
def readBlock(l):
   #the variables here are just
   #named after the init text
   #lines in the result files
   keeper                      = DataModel()
   keeper.db_nodes             = getBetween(l[1],"Nodes: ",", ")
   keeper.slaves               = getBetween(l[1],"Slaves ",", ")
   keeper.queriesSlave         = getBetween(l[1],"Queries/Slave ","\n")
   keeper.timeFromTestAverage  = getBetweenF(l[9],"Avg: ",", ")
   keeper.queriesPerSecAverage = getBetweenF(l[10],"Avg: ",", ")
   keeper.standardDeviation    = getBetweenF(l[16],"dev: ","\n")
   return keeper

def getBetween(source,left,right):
   test = source.split(left)
   lookingFor = test[1].split(right)[0]
   return int(lookingFor)

def getBetweenF(source,left,right):
   test = source.split(left)
   lookingFor = test[1].split(right)[0]
   return round(float(lookingFor),2)

def loadf(url):
   f = open(url)
   lines = f.readlines()
   f.close()
   #a list of datamodels parsed from the
   #source txt file's blocks
   models = catchBlocks(lines[2:])
   printLatexTable(models)
   #print lines

def loadfGetList(url):
   f = open(url)
   lines = f.readlines()
   f.close()
   #a list of datamodels parsed from the
   #source txt file's blocks
   return catchBlocks(lines[2:])

#Print a formatted latex table to the terminal
def printLatexTable(models):
   
   models.sort(key=lambda x:x.slaves, reverse=False)
   
   print "\\begin{longtable}{|c|c|c|c|}"
   print "\caption{A simple longtable example}\\\\"
   print "\hline "
   print "\\textbf{Slaves} & \\textbf{Nodes} & \\textbf{Queries/Sec} & \\textbf{Deviations} \\\\"
   print "\hline"
   print "\endfirsthead"
   print "\multicolumn{4}{c}%"
   print "{\\tablename\\ \\thetable\\ -- \\textit{Continued from previous page}} \\\\"
   print "\hline"
   print "\\textbf{Slaves} & \\textbf{Nodes} & \\textbf{Queries/Sec} & \\textbf{Deviations} \\\\"
   print "\hline"
   print "\endhead"
   print "\hline \multicolumn{4}{r}{\\textit{Continued on next page}} \\\\"
   print "\endfoot"
   print "\hline"
   print "\endlastfoot"
   for x in models:
      print (str(x.slaves)+" & "+
             str(x.db_nodes)+" & "+
             
             str(x.queriesPerSecAverage)+" & "+
             str(x.standardDeviation)+" \\\\")
   print "\hline"
   print "\end{longtable}"

#Print a HTML table to the terminal
def printHTMLTable(models):
   print "<html><body>"
   print '<table border="2">'
   #print the first row
   print '<tr>'
   print "<td>Nodes:</td>"
   print "<td>Slaves:</td>"
   print "<td>QueriesSlave:</td>"
   print "<td>TimeFromTestAverage:</td>"
   print "<td>QueriesPerSecAverage</td>"
   print "<td>StandardDeviation</td>"
   print "</tr>"
   for x in models:
      print "<tr>"
      print "<td>"+str(x.db_nodes)+"</td>"
      print "<td>"+str(x.slaves)+"</td>"
      print "<td>"+str(x.queriesSlave)+"</td>"
      print "<td>"+str(x.timeFromTestAverage)+"</td>"
      print "<td>"+str(x.queriesPerSecAverage)+"</td>"
      print "<td>"+str(x.standardDeviation)+"</td>"
      print "</tr>"
   print "</table></body></html>"

#Print a HTML table that can be converted directly to an excel graph
def printHTMLTableExcelGraph(models):
   
   models.sort(key=lambda x:x.slaves, reverse=True)
   
   #Set the minimum and maximum node amounts
   #so they can be looped through
   minNodeAmount = sys.maxint
   maxNodeAmount = -1
   
   allSlaveAmount = []
   for x in models:
      allSlaveAmount += [x.slaves]
      if x.db_nodes > maxNodeAmount:
         maxNodeAmount = x.db_nodes
      if x.db_nodes < minNodeAmount:
         minNodeAmount = x.db_nodes
   
   allSlaveAmount = f7(allSlaveAmount)
   
   print "<html><body>"
   print '<table border="2">'
   #print the first row
   print '<tr>'
   #Loop through the models, for each, add a new column
   #and also add the node
   print "<td>Nodes:</td>"
   
   for x in allSlaveAmount:
      print "<td>"+str(x)+" slaves</td>"
      
   print "</tr>"
   
   # AFTER FIRST ROW:::::::::
   
   #for varje amount of nodes, vill jag kolla upp varje
   #
   for y in range(0,maxNodeAmount+1):
      print '<tr>'
      print "<td>"+str(y)+"</td>"
      for z in allSlaveAmount:
         print "<td>"+findTimeForSlaveAmountAtNodeAmount(z,y,models)+"</td>"
      #loop that prints the time for all
      #slave-amounts that exist in this node amount
      print "</tr>"
   
   print "</table>"
   print "</body>"
   print "</html>"

def printHTMLTableStandardDeviation(models):
   print "<html><body>"
   print '<table border="2">'
   #print the first row
   print '<tr>'
   print "<td>Nodes:</td>"
   print "<td>Average standard deviation:</td>"
   print "</tr>"
   models.sort(key=lambda x:x.slaves, reverse=True)

   maxNodeAmount = -1
   
   for x in models:
      if x.db_nodes > maxNodeAmount:
         maxNodeAmount = x.db_nodes
   
   keeper = [(0,0)] * maxNodeAmount
      
   for z in models:
      temp = keeper[z.db_nodes-1]
      keeper[z.db_nodes-1] = (temp[0]+1,temp[1]+z.standardDeviation)
   
   uglyIncrement = 1
   
   for (times,total) in keeper:
      print "<tr>"
      print "<td>"+str(uglyIncrement)+"</td>"
      if times != 0:
         print "<td>"+str(int(round(total/times)))+"</td>"
      else:
         print "<td></td>"
      uglyIncrement = uglyIncrement + 1
      
   print "</table></body></html>"
   #print keeper

#Awkward function name but descriptive...
def findTimeForSlaveAmountAtNodeAmount(slaveAmount,nodeAmount,models):
   for x in models:
      if x.db_nodes == nodeAmount and x.slaves == slaveAmount:
         return str(int(round(x.queriesPerSecAverage)))
   return ""
   
#Just print plain text to the terminal
def printModels(models):
   for x in models:
      print "Model:"
      print ("db_nodes: "+str(x.db_nodes))
      print ("slaves: "+str(x.slaves))
      print ("queriesSlave: "+str(x.queriesSlave))
      print ("timeFromTestAverage: "+str(x.timeFromTestAverage))
      print ("queriesPerSecAverage: "+str(x.queriesPerSecAverage))
      print ("standardDeviation: "+ str(x.standardDeviation))

allModels = []
for x in (sys.argv[1:]):
   allModels += loadfGetList(x)

#print allModels

printLatexTable(allModels)
#printHTMLTableExcelGraph(allModels)
#printHTMLTableStandardDeviation(allModels)
   

